package com.wangfugui.mybatisplus.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wangfugui.mybatisplus.dao.UnrealizedPnlDao;
import com.wangfugui.mybatisplus.domain.UnrealizedPnl;
import com.wangfugui.mybatisplus.service.UnrealizedPnlService;
import com.wangfugui.mybatisplus.utils.DateUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;


@Service
@Slf4j
public class UnrealizedPnlServiceImpl extends ServiceImpl<UnrealizedPnlDao, UnrealizedPnl> implements UnrealizedPnlService {

    @Override
    public UnrealizedPnl listYesterday(Date reportDate) {
        LambdaQueryWrapper<UnrealizedPnl> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.orderByDesc(UnrealizedPnl::getDate)
                .lt(UnrealizedPnl::getDate,reportDate)
                .last("limit 1");
        List<UnrealizedPnl> list = list(queryWrapper);
        if(list.size()>0){
            return list.get(0);
        }
        return null;
    }

    @Override
    public List<UnrealizedPnl> listLastTwo(Date reportDate) {
        LambdaQueryWrapper<UnrealizedPnl> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.orderByDesc(UnrealizedPnl::getDate)
                .le(UnrealizedPnl::getDate,reportDate)
                .last("limit 2");
        List<UnrealizedPnl> list = list(queryWrapper);
        list.stream().forEach(e->{
            e.setDateStr(DateUtil.formatDate("yyyy-MM-dd",e.getDate()));
        });
        return list;
    }
}
